/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package umlapp.swingui;

import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.JPanel;
import umlapp.Drawable;
import umlapp.diagrams.Elements;
import umlapp.diagrams.Relationships;
import umlapp.elements.AElement;

/**
 *
 * @author Peter
 */
public class JDialogDelete extends javax.swing.JDialog {

    /**
     * Atribut element na prácu s drawable elementom
     */
    private AElement element;
    /**
     * Atribut panel na prácu s EditorPanel elementom
     */
    private EditorPanel panel;

    /**
     * Vytvorenie nového dialogu
     */
    public JDialogDelete(java.awt.Frame parent, boolean modal, AElement element, JPanel panel) {
        super(parent, modal);
        this.element = element;
        this.panel = (EditorPanel) panel;
        initComponents();
        setLocationRelativeTo(this);
    }

    /**
     * Pomocná metóda, ktorá z obsahu content vymazáva všetky vťahy daného elementu
     *
     * @param element z ktorého sa vymazávaju vzťahy
     */
    private void deleteRelationShips(Drawable element) {
        ArrayList<AElement> elements = new ArrayList<>();
        for (Iterator<AElement> it = (panel).getContent().iterator(); it.hasNext();) {
            AElement h = it.next();
            if (h instanceof Relationships) {
                if (((Relationships) h).getEndDrawable().equals(element) || ((Relationships) h).getStartDrawable().equals(element)) {
                    elements.add(h);
                }
            }
        }
        for (AElement h : elements) {
            (panel).removeDrawableElement(h);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form. WARNING: Do NOT
     * modify this code. The content of this method is always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jButtonOk = new javax.swing.JButton();
        jButtonCancel = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Are you sure? ");

        jButtonOk.setText("OK");
        jButtonOk.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonOkActionPerformed(evt);
            }
        });

        jButtonCancel.setText("Cancel");
        jButtonCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCancelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButtonOk)
                        .addGap(18, 18, 18)
                        .addComponent(jButtonCancel))
                    .addComponent(jLabel1))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonOk)
                    .addComponent(jButtonCancel))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * Implementácia tlačidla Ok
     *
     * @param evt
     */
    private void jButtonOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonOkActionPerformed
        if (element instanceof Elements) {
            deleteRelationShips(element);
        }
        (panel).removeDrawableElement((AElement) element);
        panel.repaint();
        setVisible(false);
        dispose();
    }//GEN-LAST:event_jButtonOkActionPerformed
    /**
     * Implementácia tlačidla cancel
     *
     * @param evt
     */
    private void jButtonCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCancelActionPerformed
        setVisible(false);
        dispose();
    }//GEN-LAST:event_jButtonCancelActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonCancel;
    private javax.swing.JButton jButtonOk;
    private javax.swing.JLabel jLabel1;
    // End of variables declaration//GEN-END:variables
}
